package com.shop.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.MerchantEmployee;
import com.shop.entity.MerchantRole;
import com.shop.mapper.MerchantEmployeeMapper;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 供应商  供货商列表的信息由采购后自动增加 yyt(MerchantEmployee)表服务接口
 *
 * @author makejava
 * @since 2020-11-25 17:42:39
 */
@Service
@AllArgsConstructor
public class MerchantEmployeeService {

    private final MerchantEmployeeMapper merchantEmployeeMapper;

    private final MerchantRoleService merchantRoleService;

    public Rsp getEmployeeInfoList(PageParam<MerchantEmployee> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        List<MerchantEmployee> employeeInfoList = merchantEmployeeMapper.getEmployeeInfoList();
        PageInfo<MerchantEmployee> pageInfo = new PageInfo<>(employeeInfoList);
        return !StringUtils.isEmpty(pageInfo.getList()) ? Rsp.success(pageInfo) : Rsp.fail();
    }

    public Rsp insertEmp(Integer userId,MerchantEmployee merchantEmployee) {
        MerchantRole role = merchantRoleService.getRoleById(merchantEmployee.getRoleId());
        if (!StringUtils.isEmpty(role) && !StringUtils.isEmpty(userId)) {
            merchantEmployee.setRole(role.getName());
            merchantEmployee.setRoleSign(role.getSign());
            merchantEmployee.setAvatar("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1606042436823&di=4d1b41d55751ce5a0524d6b801947eb6&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F27%2F81%2F01200000194677136358818023076.jpg");
            merchantEmployee.setStatus("0");
            merchantEmployee.setCreateUserId(userId);
        }
        int res = merchantEmployeeMapper.insert(merchantEmployee);
        return res > 0 ? Rsp.success(res) : Rsp.fail();
    }

    public Rsp updateEmp(Integer userId,MerchantEmployee merchantEmployee) {
        MerchantRole role = merchantRoleService.getRoleById(merchantEmployee.getRoleId());
        if (!StringUtils.isEmpty(role) && !StringUtils.isEmpty(userId)) {
            merchantEmployee.setRole(role.getName());
            merchantEmployee.setRoleSign(role.getSign());
            merchantEmployee.setUpdateUserId(userId);
        }
        Integer res = merchantEmployeeMapper.updateEmp(merchantEmployee);
        return res > 0 ? Rsp.success(res) : Rsp.fail();
    }

    public Rsp deleteEmp(Integer userId,MerchantEmployee merchantEmployee){
        int res = merchantEmployeeMapper.deleteById(merchantEmployee.getId());
        return res > 0 ? Rsp.success(res) : Rsp.fail();
    }
}
